package com.client.handler;

import com.codec.AppConfigDecoder;
import com.codec.AppConfigEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author tangj
 * @date 2018/5/6 22:54
 */
@Component
public class ClientInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    ClientHandler clientHandler;

    public ClientInitializer(ClientHandler clientHandler) {
        this.clientHandler = clientHandler;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder",new AppConfigDecoder());
        pipeline.addLast("encoder",new HttpRequestEncoder());
        pipeline.addLast("handler",clientHandler);
        // WebSocketClientHandshaker00
//        pipeline.addLast(new WebSocketClientProtocolHandler());
    }
}
